﻿using System;

namespace iMaker.Library
{
    /// <summary>
    /// 分页类，需要接受外部参数：curPage,pageSize,从数据层返回时要给recordSum填充值
    /// </summary>
    public class PagerCollection
    {
        private string _pageParam = "page"; //URL分页参数
        private int _curPage = 1;             //当前页码
        private int _recordStart = 0;         //当前记录开始的位置
        private int _prePage = 1;             //前一页码
        private int _nextPage = 1;            //下一页码
        private string _curPageLink = string.Empty;//当前页链接
        private string _prePageLink = string.Empty;//前一页的链接
        private string _nextPageLink = string.Empty;//下一页链接
        private int _pageSize = 1;            //页面大小
        private int _pageCount = 0;           //总页数
        private int _recordSum = 0;           //总记录数
        private string _param = string.Empty; //URL 其它参数
        private string _curPageName = string.Empty;//当前页面名字 


        public PagerCollection()
        {
            Param = getParamString();
            CurPageName = System.Web.HttpContext.Current.Request.Path.ToString();

            if (!string.IsNullOrEmpty(Param))
            {
                _curPageLink = CurPageName + "?" + Param + "&" + _pageParam + "=" + _curPage.ToString();
            }
            else
            {
                _curPageLink = CurPageName + "?" + _pageParam + "=" + _curPage.ToString();
            }
        }

        public PagerCollection(int iPageSize)
        {
            PageSize = iPageSize;
            Param = getParamString();
            CurPageName = System.Web.HttpContext.Current.Request.Path.ToString();

            if (!string.IsNullOrEmpty(Param))
            {
                _curPageLink = CurPageName + "?" + Param + "&" + _pageParam + "=" + _curPage.ToString();
            }
            else
            {
                _curPageLink = CurPageName + "?" + _pageParam + "=" + _curPage.ToString();
            }
        }

        public string PageParam
        {
            get { return _pageParam; }
            set { _pageParam = value; }
        }
        public int CurPage
        {
            get {
                if (PageCount == 0)
                    return 0;
                else
                    return _curPage; 
            }
            set
            {
                _curPage = value; 
            }
        }
        public int RecordStart
        {
            get { 
                _recordStart = (CurPage - 1)*PageSize;
                return _recordStart; 
            }
            set { _recordStart = value; }
        }
        public int PrePage
        {
            get { return _prePage; }
        }  
        public int NextPage
        {
            get { return _nextPage; }
        }
        public string CurPageLink
        {
            get { return _curPageLink; }
        }
        public string PrePageLink
        {
            get { return _prePageLink; }
        }
        public string NextPageLink
        {
            get { return _nextPageLink; }
        }
        public int PageCount
        {
            get { return _pageCount; }
        }
        public int RecordSum
        {
            get { return _recordSum; }
            set { 
                _recordSum = value;
                //更新总数
                _pageCount = int.Parse(Math.Ceiling(double.Parse(RecordSum.ToString()) / double.Parse(PageSize.ToString())).ToString());

                //更新信息
                Refresh();
            }
        }
        public int PageSize
        {
            get { return _pageSize; }
            set { 
                _pageSize = value;
                if (_pageSize == 0)
                    _pageSize = 1;
            }
        }       
        public string Param
        {
            get { return _param; }
            set { _param = value; }
        }
        public string CurPageName
        {
            get { return _curPageName; }
            set { _curPageName = value; }
        }
        /// <summary>
        /// 获取当前页面除page参数外的其它参数
        /// </summary>
        /// <returns></returns>
        public string getParamString()
        {
            string strParams = System.Web.HttpContext.Current.Request.QueryString.ToString();
            string strRtn = string.Empty;

            if (strParams.Trim() != string.Empty)
            {
                string[] strArray = strParams.Split('&');
                for(int i=0;i<strArray.Length;i++)
                {
                    if (!strArray[i].Contains(this.PageParam))
                        strRtn += "&"+strArray[i];
                }
            }
            if (strRtn.StartsWith("&")) strRtn=strRtn.Substring(1);
            return strRtn;
        }
        /// <summary>
        /// 更新前一页数,后一页数及其对应链接
        /// </summary>
        public void Refresh()
        {
            CurPageName = System.Web.HttpContext.Current.Request.Path.ToString();
            if (!string.IsNullOrEmpty(Param))
            {
                _curPageLink = CurPageName + "?" + Param + "&" + _pageParam + "=" + _curPage.ToString();
            }
            else
            {
                _curPageLink = CurPageName + "?" + _pageParam + "=" + _curPage.ToString();
            }

            if (CurPage > 0)
            {
                _prePage = CurPage - 1;
            }
            else
            {
                _prePage = 0;
            }
            if (CurPage < PageCount)
            {
                _nextPage = CurPage + 1;
            }
            else
            {
                _nextPage = PageCount;
            }
            if (Param.Trim() != string.Empty)
            {
                _prePageLink = CurPageName + "?" + Param + "&" + PageParam + "=" + PrePage;
                _nextPageLink = CurPageName + "?" + Param + "&" + PageParam + "=" + NextPage;
            }
            else
            {
                _prePageLink = CurPageName + "?" + PageParam + "=" + PrePage;
                _nextPageLink = CurPageName + "?" + PageParam + "=" + NextPage;
            }

        }
    }
}
